package com.ruoyi.approval.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.apply.domain.SysSimApply;
import com.ruoyi.apply.service.ISysSimApplyService;
import com.ruoyi.receive.domain.SimReceive;
import com.ruoyi.receive.service.ISimReceiveService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.approval.domain.SysSimApproval;
import com.ruoyi.approval.service.ISysSimApprovalService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 审批记录Controller
 *
 * @author laifolai
 * @date 2023-02-06
 */
@RestController
@RequestMapping("/approval/manage")
public class SysSimApprovalController extends BaseController {
    @Autowired
    private ISysSimApprovalService sysSimApprovalService;

    @Autowired
    ISysSimApplyService sysSimApplyService;

    @Autowired
    ISimReceiveService simReceiveService;

    /**
     * 查询审批记录列表
     */
    //@PreAuthorize("@ss.hasPermi('approval:manage:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysSimApproval sysSimApproval) {
        startPage();
        List<SysSimApproval> list = sysSimApprovalService.selectSysSimApprovalList(sysSimApproval);
        return getDataTable(list);
    }

    /**
     * 导出审批记录列表
     */
    //@PreAuthorize("@ss.hasPermi('approval:manage:export')")
    @Log(title = "审批记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysSimApproval sysSimApproval) {
        List<SysSimApproval> list = sysSimApprovalService.selectSysSimApprovalList(sysSimApproval);
        ExcelUtil<SysSimApproval> util = new ExcelUtil<SysSimApproval>(SysSimApproval.class);
        util.exportExcel(response, list, "审批记录数据");
    }

    /**
     * 获取审批记录详细信息
     */
    //@PreAuthorize("@ss.hasPermi('approval:manage:query')")
    @GetMapping(value = "/{approvalId}")
    public AjaxResult getInfo(@PathVariable("approvalId") Long approvalId) {
        return AjaxResult.success(sysSimApprovalService.selectSysSimApprovalByApprovalId(approvalId));
    }

    /**
     * 新增审批记录
     */
    //@PreAuthorize("@ss.hasPermi('approval:manage:add')")
    @Log(title = "审批记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysSimApproval sysSimApproval) {
        return toAjax(sysSimApprovalService.insertSysSimApproval(sysSimApproval));
    }

    /**
     * 新增审批记录
     */
    //@PreAuthorize("@ss.hasPermi('approval:manage:add')")
    @Log(title = "审批记录", businessType = BusinessType.INSERT)
    @PostMapping(value = "/hand")
    public AjaxResult hand(@RequestBody SysSimApproval sysSimApproval) {
        AjaxResult ajaxResult = toAjax(sysSimApprovalService.insertSysSimApproval(sysSimApproval));

        SysSimApproval resultApproval = sysSimApprovalService.selectSysSimApprovalList(sysSimApproval).get(0);

        SysSimApply simApply = sysSimApplyService.selectSysSimApplyByApplyId(sysSimApproval.getApplyId());
        Long result = sysSimApproval.getApprovalResult();
        if (result == 0) {
            simApply.setStatus(2L);
        } else {
            //同意，生成待领用记录
            SimReceive simReceive = new SimReceive();
            simReceive.setApprovalId(resultApproval.getApprovalId());
            simReceive.setApplyId(simApply.getApplyId());
            simReceive.setReceiveUser(simApply.getApplyUser());
            simReceive.setReceiveStatus(0L);
            simReceiveService.insertSimReceive(simReceive);
            //申请状态改为已同意
            simApply.setStatus(1L);
        }

        sysSimApplyService.updateSysSimApply(simApply);

        return ajaxResult;
    }


    /**
     * 修改审批记录
     */
    //@PreAuthorize("@ss.hasPermi('approval:manage:edit')")
    @Log(title = "审批记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysSimApproval sysSimApproval) {
        return toAjax(sysSimApprovalService.updateSysSimApproval(sysSimApproval));
    }

    /**
     * 删除审批记录
     */
    //@PreAuthorize("@ss.hasPermi('approval:manage:remove')")
    @Log(title = "审批记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{approvalIds}")
    public AjaxResult remove(@PathVariable Long[] approvalIds) {
        return toAjax(sysSimApprovalService.deleteSysSimApprovalByApprovalIds(approvalIds));
    }
}
